package com.sugar.persistence.ibatis;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import com.sugar.base.core.MyBatisConnSupport;
import com.sugar.domain.ibatis.IbatisDemo;

@Repository
public class IbatisDemoDao implements IbatisDemoMapper {

	@Resource
	private MyBatisConnSupport myibatis;

	@Override
	public void deleteIbatisDemoByIdMapper(int id) {
		SqlSession session = this.myibatis.getSqlSession() ;
		session.delete("ibatis.resource.deleteIbatis", "1") ;
		System.out.println("111");
		session.commit() ;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<IbatisDemo> findAllIbatisDemoMapper() {
		SqlSession session = this.myibatis.getSqlSession() ;
		
		List s = (List) session.selectList("ibatis.resource.findAllIbatis") ;
		
		for (Iterator iterator = s.iterator(); iterator.hasNext();) {
			IbatisDemo ibatisDemo = (IbatisDemo) iterator.next();
			System.out.println(ibatisDemo.getIbatis_name()) ;
		}
		return null;
	}

	@Override
	public IbatisDemo findIbatisDemoByIdMapper(int id) {
		SqlSession session = this.myibatis.getSqlSession() ;
		IbatisDemo s = (IbatisDemo) session.selectOne("ibatis.resource.loadIbatis",id) ;
		System.out.println(s.getIbatis_name());
		return null;
	}

	@Override
	public void saveStudentMapper(IbatisDemo demo) {
		SqlSession session = this.myibatis.getSqlSession() ;
		IbatisDemo de = new IbatisDemo();
		de.setIbatis_id(1);
		de.setIbatis_name("杨浩泉");

		session.insert("ibatis.resource.saveIbatis", de);
		session.commit();
	}

	@Override
	public void updateIbatisDemoByIdMapper(IbatisDemo demo) {
		SqlSession session = this.myibatis.getSqlSession() ;
		
		IbatisDemo de = new IbatisDemo();
		de.setIbatis_id(1);
		de.setIbatis_name("努尔哈赤");
		
		session.update("ibatis.resource.updateIbatis", de) ;
		session.commit();
	}

	public static void main(String[] args) {

		ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("app*.xml");

		IbatisDemoMapper d = (IbatisDemoMapper) c.getBean("ibatisDemoDao");

		d.saveStudentMapper(null);
		
		//d.deleteIbatisDemoByIdMapper(1) ;
		
		//d.findIbatisDemoByIdMapper(1) ;
		
		//--d.findAllIbatisDemoMapper();
		
		//--d.updateIbatisDemoByIdMapper(null) ;
	}

}
